<?php
class MmshopController extends CController {
	public function actionIos() {
		$appid = 'wxc6066b87ac65dace';
		$appSecret = '3a46403cb632d358c8f855f47028d4a7';
		if (! isset ( $_SESSION ['opid'] )) {
			
			$code = Tool::getrequestValue ( 'code' );
			
			if (! $code) {
				header ( "location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6066b87ac65dace&redirect_uri=http%3a%2f%2fwq.weixin.nicoworks.com%2findex.php%3fr%3dmmshop%2fIos&response_type=code&scope=snsapi_base&state=123" );
				exit ();
			} else {
				$opid = WeixinApi::getaccess_token ( $appid, $appSecret, $code );
				$_SESSION ['opid'] = $opid;
				if ($opid == '获取access_token错误') {
					echo "getCode Error:access_token error";
					exit ();
				}
			}
		}
		else
		$opid = $_SESSION ['opid'];
		
		require 'jssdk.php';
		$jssdk = new JSSDK ( $appid, $appSecret );
		$signPackage = $jssdk->GetSignPackage ();
		
		$this->render ( 'view_ios', array (
				'user_openid' => $opid,
				'signPackage' => $signPackage 
		) );
	}
	
	
	public function actionAndroid() {
	    $appid = 'wxc6066b87ac65dace';
	    $appSecret = '3a46403cb632d358c8f855f47028d4a7';
	    if (! isset ( $_SESSION ['opid'] )) {
	        	
	        $code = Tool::getrequestValue ( 'code' );
	        	
	        if (! $code) {
	            header ( "location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6066b87ac65dace&redirect_uri=http%3a%2f%2fwq.weixin.nicoworks.com%2findex.php%3fr%3dmmshop%2fAndroid&response_type=code&scope=snsapi_base&state=123" );
	            exit ();
	        } else {
	            $opid = WeixinApi::getaccess_token ( $appid, $appSecret, $code );
	            $_SESSION ['opid'] = $opid;
	            if ($opid == '获取access_token错误') {
	                echo "getCode Error:access_token error";
	                exit ();
	            }
	        }
	    }
	    else
	        $opid = $_SESSION ['opid'];
	
	    require 'jssdk.php';
	    $jssdk = new JSSDK ( $appid, $appSecret );
	    $signPackage = $jssdk->GetSignPackage ();
	
	    $this->render ( 'view_android', array (
	        'user_openid' => $opid,
	        'signPackage' => $signPackage
	    ) );
	}
	
	public function actionShow() {
		$appid = 'wxc6066b87ac65dace';
		$appSecret = '3a46403cb632d358c8f855f47028d4a7';
		require 'jssdk.php';
		$jssdk = new JSSDK ( $appid, $appSecret );
		$signPackage = $jssdk->GetSignPackage ();
		
		$this->render ( 'test', array (
				'user_openid' => '9999',
				'signPackage' => $signPackage 
		) );
	}
	
	public function actionGetCodeIos() {
		$openid = Tool::getrequestValue ( 'openid' );
		
		if (empty ( $openid )) {
			echo '{"code":3,"msg":"领取错误!"}';
			Yii::app ()->end ();
		}
		
		$check = MmshopIos::model ()->find ( "openid =? and status=1", array (
				$openid 
		) );
		if (! empty ( $check )) {
			echo '{"code":4,"msg":"您已领取过了.","code_sn":"' . $check->code . '"}';
			Yii::app ()->end ();
		}
		
		$ex = MmshopIos::model ()->find ( "openid is null and status=1" );
		if (empty ( $ex )) {
			echo '{"code":3,"msg":"兑换码已领完"}';
			Yii::app ()->end ();
		}
		
		$ex->openid = $openid;
		$ex->save ();
		
		echo '{"code":0,"msg":"' . $ex->code . '"}';
	}
	
	public function actionGetCodeAndroid() {
	    $openid = Tool::getrequestValue ( 'openid' );
	
	    if (empty ( $openid )) {
	        echo '{"code":3,"msg":"领取错误!"}';
	        Yii::app ()->end ();
	    }
	
	    $check = MmshopAndroid::model ()->find ( "openid =? and status=1", array (
	        $openid
	    ) );
	    if (! empty ( $check )) {
	        echo '{"code":4,"msg":"您已领取过了.","code_sn":"' . $check->code . '"}';
	        Yii::app ()->end ();
	    }
	
	    $ex = MmshopAndroid::model ()->find ( "openid is null and status=1" );
	    if (empty ( $ex )) {
	        echo '{"code":3,"msg":"兑换码已领完"}';
	        Yii::app ()->end ();
	    }
	
	    $ex->openid = $openid;
	    $ex->save ();
	
	    echo '{"code":0,"msg":"' . $ex->code . '"}';
	}
}
